package com.itheima.controller;

import com.itheima.annotation.LogOperation;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.ClazzQueryParam;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/***
 * 班级管理
 */
@RestController
@RequestMapping("/clazzs")
public class ClazzController {

    @Autowired
    private ClazzService clazzService;


    /***
     * 查询所有班级
     */
    @GetMapping("/list")
    public Result list(){
        //调用Service层查询所有班级
        List<Clazz> list = clazzService.list();
        return Result.success(list);
    }


    /***
     * 根据ID批量删除
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        //调用Service完成删除
        clazzService.delete(Arrays.asList(id));
        return Result.success();
    }


    /***
     * 修改班级
     */
    @PutMapping
    public Result update(@RequestBody Clazz clazz){
        //调用Service完成修改
        clazzService.update(clazz);
        return Result.success();
    }

    /***
     * 修改操作-根据ID查询回显
     */
    @GetMapping("/{id}")
    public Result getClazzInfo(@PathVariable Integer id){
        //调用Service层实现查询
        Clazz clazz = clazzService.getClazzInfo(id);
        return Result.success(clazz);
    }


    /**
     * 新增班级
     */
    @LogOperation
    @PostMapping
    public Result add(@RequestBody Clazz clazz){
        //调用Service完成新增
        clazzService.add(clazz);
        return Result.success();
    }

    /***
     * 分页条件查询班级信息
     */
    @GetMapping
    public Result page(ClazzQueryParam clazzQueryParam){
        //查询数据
        PageResult pageResult = clazzService.page(clazzQueryParam);
        return Result.success(pageResult);
    }
}
